//
//  WTCommentTableViewCell.m
//  OneDay
//
//  Created by lanou on 16/1/13.
//  Copyright © 2016年 小明. All rights reserved.
//

#import "WTCommentTableViewCell.h"
#import <UIImageView+WebCache.h>

#define kMargin  15 * kRelativeLength_X

#define kUserImageVWidth  30 * kRelativeLength_X

#define kUserNameLabelHeight 20 * kRelativeLength_X

@implementation WTCommentTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // 头像
        _userImageV = [[UIImageView alloc]initWithFrame:CGRectMake(kMargin, kMargin, kUserImageVWidth, kUserImageVWidth)];
        _userImageV.layer.cornerRadius = kUserImageVWidth / 2;
        _userImageV.backgroundColor = [UIColor redColor];
        _userImageV.layer.masksToBounds = YES;
        [self.contentView addSubview:_userImageV];
        
      
        // 姓名
        _userNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(_userImageV.right + kMargin, _userImageV.top + (kUserImageVWidth - kUserNameLabelHeight) / 2, 200 * kRelativeLength_X, kUserNameLabelHeight)];
        _userNameLabel.font = [UIFont systemFontOfSize:16];
        _userNameLabel.textColor = [UIColor blueColor];
        [self.contentView addSubview:_userNameLabel];
        
        // 军衔
        _militaryRankLabel = [[UILabel alloc]initWithFrame:CGRectMake(_userNameLabel.right + kMargin, _userNameLabel.top, 100 * kRelativeLength_X, _userNameLabel.height)];
        _militaryRankLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:_militaryRankLabel];
        
        // 楼层
        _loushuLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth - kRelativeLength_X * 100 - kMargin, _militaryRankLabel.top, 100 * kRelativeLength_X, kUserNameLabelHeight)];
        _loushuLabel.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:_loushuLabel];
        
        
        // 用于存放回复内容的view
        _replyView = [[UIView alloc]initWithFrame:CGRectMake(kMargin * 3, _userNameLabel.bottom + kMargin, kScreenWidth - kMargin * 6, _userNameLabel.height * 2 + kMargin * 3)];
        _replyView.backgroundColor = [UIColor grayColor];
        _replyView.alpha = 0.7;
        [self.contentView addSubview:_replyView];
        
        // 被引用人的姓名
        _replysUserNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, kMargin, _replyView.width - kMargin * 2, _userNameLabel.height)];
        [_replyView addSubview:_replysUserNameLabel];
        
        // 被引用人的内容
        _replysLabel = [[UILabel alloc]initWithFrame:CGRectMake(kMargin, _replysUserNameLabel.bottom + kMargin, _replysUserNameLabel.width, _userNameLabel.height)];
        _replysLabel.font = [UIFont systemFontOfSize:14];
        _replysLabel.numberOfLines = 0;
        [_replyView addSubview:_replysLabel];
        
        
        // 内容
        _contmentLabel = [[UILabel alloc]initWithFrame:CGRectMake(kMargin, _replyView.bottom + kMargin, kScreenWidth - kMargin * 2, kMargin)];
        _contmentLabel.font = [UIFont systemFontOfSize:14];
        _contmentLabel.numberOfLines = 0;
        [self.contentView addSubview:_contmentLabel];
        
        // 发布时间
        _publishTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(_contmentLabel.left, _contmentLabel.bottom + kMargin, 150 * kRelativeLength_X, kUserNameLabelHeight)];
        [self.contentView addSubview:_publishTimeLabel];
        
        // 下面那条横线
        _imageV = [[UIImageView alloc]initWithFrame:CGRectMake(kMargin, _publishTimeLabel.bottom + kMargin - kRelativelength_Y , kScreenWidth - 2 * kMargin, kRelativelength_Y)];
        _imageV.backgroundColor = [UIColor blackColor];
        [self.contentView addSubview:_imageV];
      
    }
    return self;
}


#pragma mark -- 重写model的set方法
- (void)setModel:(WTRecommendDetailModel *)model
{
    _model = model;
    
    // 给头像赋值
    [_userImageV sd_setImageWithURL:[NSURL URLWithString:model.userImg] placeholderImage:nil];
    if ([model.loushu isEqualToString:@""]) {
         _loushuLabel.text = [NSString stringWithFormat:@"%@",model.loushu];
    }else {
         _loushuLabel.text = [NSString stringWithFormat:@"%@楼",model.loushu];
    }
   
    // 自适应用户名的长度
    NSDictionary *userNameDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16],NSFontAttributeName, nil];
    CGRect userFrame = [model.userName boundingRectWithSize:CGSizeMake(kScreenWidth, _userNameLabel.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:userNameDic context:nil];
    
    // 给用户赋值名字以及修改长度
    _userNameLabel.frame = CGRectMake(_userNameLabel.left, _userNameLabel.top, userFrame.size.width, _userNameLabel.height);
    _userNameLabel.text = model.userName;
    
    
    // 根据用户名的label修改军衔的label
    _militaryRankLabel.frame = CGRectMake(_userNameLabel.right + kMargin, _userNameLabel.top, kScreenWidth - _userNameLabel.right - kMargin, _userNameLabel.height);
    _militaryRankLabel.text = [NSString stringWithFormat:@"军衔:%@",model.militaryRank];

#pragma mark -- 判断有没有回复别人
    
    if (model.replyContent != nil) {
    
        // 有回复别人的内容
        _replysUserNameLabel.frame = CGRectMake(kMargin, kMargin, _replysUserNameLabel.width, kUserNameLabelHeight);
        _replysUserNameLabel.text = model.replyUserName;
        
        // 自适应引用别人的内容
        NSDictionary *replyContentDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName, nil];
        CGRect replycontent = [model.replyContent boundingRectWithSize:CGSizeMake(_replysUserNameLabel.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:replyContentDic context:nil];
        _replysLabel.frame = CGRectMake(_replysLabel.left, _replysLabel.top, _replysLabel.width, replycontent.size.height);
        _replysLabel.text = model.replyContent;
        
        // 修改整个引用区域的view的高度
        _replyView.frame = CGRectMake(_replyView.left, _replyView.top, _replyView.width, 3 * kMargin + kUserNameLabelHeight + replycontent.size.height);
        _replyView.hidden = NO;
        _replyView.alpha = 0.7;
        
    } else{
        _replyView.frame = CGRectMake(_replyView.left, _replyView.top, _replyView.width, 0);
        _replyView.hidden = YES;
        _replyView.alpha = 0;
    }
    
    
    // 自适应内容
    NSDictionary * contentDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName, nil];
    CGRect contentFrame = [model.content boundingRectWithSize:CGSizeMake(_contmentLabel.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:contentDic context:nil];
    // 判断有没有引用别人
    if (_replyView.height == 0) {
        
        _contmentLabel.frame = CGRectMake(_contmentLabel.left, _userNameLabel.bottom + kMargin, _contmentLabel.width, contentFrame.size.height);
        _contmentLabel.text = model.content;
        
    } else{
       
        _contmentLabel.frame = CGRectMake(_contmentLabel.left, _replyView.bottom + kMargin, _contmentLabel.width, contentFrame.size.height);
        _contmentLabel.text = model.content;
    }
    
    // 给时间赋值
    _publishTimeLabel.frame = CGRectMake(_publishTimeLabel.left, _contmentLabel.bottom + kMargin, _publishTimeLabel.width, _publishTimeLabel.height);
    _publishTimeLabel.text = model.publishTime;
    
    
    _imageV.frame = CGRectMake(kMargin, _publishTimeLabel.bottom + kMargin - kRelativelength_Y, _imageV.width, kRelativelength_Y);

}

// 返回cell的高度
+ (CGFloat)heightForRowWithModel:(WTRecommendDetailModel *)model
{
    
    CGFloat height = 0;
    
#pragma mark -- 判断有没有回复别人
    if (model.replyContent != nil) {
        
        // 自适应引用别人的内容
        NSDictionary *replyContentDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName, nil];
        CGRect replycontent = [model.replyContent  boundingRectWithSize:CGSizeMake(kScreenWidth - 8 * kMargin, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:replyContentDic context:nil];
      
        height = kMargin + kUserNameLabelHeight + kMargin+ kMargin + kUserNameLabelHeight + kMargin + replycontent.size.height + kMargin + kMargin;
    } else{
        
        height = kMargin + kUserNameLabelHeight + kMargin;
    }
    
    // 获得自适应内容的高度
    NSDictionary * contentDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName, nil];
    CGRect contentFrame = [model.content boundingRectWithSize:CGSizeMake(kScreenWidth - kMargin * 2, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:contentDic context:nil];
    height = height + contentFrame.size.height + kMargin + kUserNameLabelHeight + kMargin;
    
    // 返回cell的高度
    return height;
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
